﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Web;
using System.Web.UI;

namespace AAA.Web
{

    public class CellRange
    {
        private int _iStartIndex;
        private int _iEndIndex;

        public int StartIndex
        {
            get { return _iStartIndex; }
            set { _iStartIndex = value; }
        }

        public int EndIndex
        {
            get { return _iEndIndex; }
            set { _iEndIndex = value; }
        }

        public CellRange()
            : this(0, 0)
        {
        }

        public CellRange(int iStartRow, int iEndRow)
        {
            StartIndex = iStartRow;
            EndIndex = iEndRow;
        }
    }

    public class GridViewHelper
    {
        public static void SetBackColor(GridView gvSource, int iRefCol, string strIdentifyText, string strColor)
        {
            SetBackColor(gvSource, iRefCol, strIdentifyText, strColor, 0, gvSource.Rows.Count - 1);
        }

        public static void SetBackColor(GridView gvSource, int iRefCol, string strIdentifyText, string strColor, int iStartRow, int iEndRow)
        {
            for (int i = iEndRow; i >= iStartRow; i--)
            {
                if (gvSource.Rows[i].Cells[iRefCol].Text.Trim() != strIdentifyText)
                    continue;

                for (int j = 0; j < gvSource.Columns.Count; j++)
                    gvSource.Rows[i].Cells[j].BackColor = Color.FromName(strColor);
            }
        }

        public static void MergeRowByRefSpanedCol(GridView gvSource, int iRefCol, int[] iCols)
        {
            MergeRowByRefSpanedCol(gvSource, iRefCol, iCols, 0, gvSource.Rows.Count);
        }

        public static void MergeRowByRefSpanedCol(GridView gvSource, int iRefCol, int[] iCols, int iStartRow, int iEndRow)
        {
            for (int i = iEndRow - 1; i >= iStartRow; i--)
            {
                try
                {
                    if (gvSource.Rows[i].Cells[iRefCol].RowSpan > 1)
                    {
                        for (int j = 1; j < gvSource.Rows[i].Cells[iRefCol].RowSpan; j++)
                            for(int k = 0; k < iCols.Length; k++)
                                gvSource.Rows[i + j].Cells[iCols[k]].Visible = false;
                        for (int j = 0; j < iCols.Length; j++)
                            gvSource.Rows[i].Cells[iCols[j]].RowSpan = gvSource.Rows[i].Cells[iRefCol].RowSpan;
                    }
                }
                catch { }
            }
        }

        public static void MergeRowByRefCol(GridView gvSource, int iRefCol, int[] iCols)
        {
            MergeRowByRefCol(gvSource, iRefCol, iCols, 0, gvSource.Rows.Count);
        }

        public static void MergeRowByRefCol(GridView gvSource, int iRefCol, int[] iCols, int iStartRow, int iEndRow)
        {
            CellRange cellRange = new CellRange();
            string strPreviousText = "";
            int iRowSpan = 1;

            if (iRefCol < 0)
                return;

            try
            {
                cellRange.StartIndex = iEndRow - 1;
                strPreviousText = gvSource.Rows[iEndRow - 1].Cells[iRefCol].Text;
            }
            catch { }
            for (int i = iEndRow - 1; i >= iStartRow; i--)
            {
                
//                if (gvSource.Rows[i].Cells[iRefCol].Text == "")
//                    continue;

                if ((gvSource.Rows[i].Cells[iRefCol].Text == strPreviousText))
                {

                    if (cellRange.StartIndex == 0)
                        cellRange.StartIndex = i;

                    cellRange.EndIndex = i;
                    continue;
                }

                strPreviousText = gvSource.Rows[i].Cells[iRefCol].Text;

                if ((cellRange.StartIndex == cellRange.EndIndex) ||
                    (cellRange.StartIndex == 0) ||
                    (cellRange.EndIndex == 0))
                {
                    cellRange = new CellRange(i, iStartRow);                    
                    continue;
                }


                for(int j = cellRange.StartIndex; j > cellRange.EndIndex; j--)
                {
                    gvSource.Rows[j].Cells[iRefCol].Visible = false;
                    for(int k = 0; k < iCols.Length; k++)
                        gvSource.Rows[j].Cells[iCols[k]].Visible = false;
                }

                iRowSpan = (cellRange.StartIndex - cellRange.EndIndex) + 1;
                gvSource.Rows[cellRange.EndIndex].Cells[iRefCol].RowSpan = iRowSpan;

                for (int j = 0; j < iCols.Length; j++)
                    gvSource.Rows[cellRange.EndIndex].Cells[iCols[j]].RowSpan = iRowSpan;

                cellRange = new CellRange(i, iStartRow);
            }

            for (int j = cellRange.StartIndex; j > cellRange.EndIndex; j--)
            {
                gvSource.Rows[j].Cells[iRefCol].Visible = false;
                for (int k = 0; k < iCols.Length; k++)
                    gvSource.Rows[j].Cells[iCols[k]].Visible = false;
            }

            iRowSpan = (cellRange.StartIndex - cellRange.EndIndex) + 1;
            gvSource.Rows[cellRange.EndIndex].Cells[iRefCol].RowSpan = iRowSpan;

            for (int j = 0; j < iCols.Length; j++)
                gvSource.Rows[cellRange.EndIndex].Cells[iCols[j]].RowSpan = iRowSpan;

        }

        public static void MergeRow(GridView gvSource, int iCol)
        {
            MergeRow(gvSource, iCol, iCol, 0, gvSource.Rows.Count - 2);
        }

        public static void MergeRow(GridView gvSource, int iStartCol, int iEndCol)
        {
            MergeRow(gvSource, iStartCol, iEndCol, 0, gvSource.Rows.Count - 2);
        }

        public static void MergeRow(GridView gvSource, int iStartCol, int iEndCol, int iStartRow, int iEndRow)
        {
            for (int i = iStartCol; i <= iEndCol; i++)
            {
                if (i > 0)
                {
                    List<CellRange> lstRange = new List<CellRange>();

                    TraversesCol(gvSource, i - 1, lstRange, iStartRow, iEndRow);
                    foreach (CellRange item in lstRange)
                    {
                        MergeRow(gvSource, i, item.StartIndex, item.EndIndex);
                    }
                }
                else
                {
                    MergeRow(gvSource, i, iStartRow, iEndRow);
                }
            }
        }

        private static void TraversesCol(GridView gvSource, int iCol, List<CellRange> lstCellRange, int iStartRow, int iEndRow)
        {
            CellRange cell = null;
            for (int i = iStartRow; i <= iEndRow; i++)
            {
                if (!gvSource.Rows[i].Cells[iCol].Visible)
                    continue;

                cell = new CellRange();
                cell.StartIndex = gvSource.Rows[i].RowIndex;
                cell.EndIndex = cell.StartIndex + gvSource.Rows[i].Cells[iCol].RowSpan - 2;
                lstCellRange.Add(cell);
            }
        }

        private static void MergeRow(GridView gvSource, int iCol, int iStartRow, int iEndRow)
        {
            for (int iRowIndex = iEndRow; iRowIndex >= iStartRow; iRowIndex--)
            {
                GridViewRow currentRow = gvSource.Rows[iRowIndex];
                GridViewRow prevRow = gvSource.Rows[iRowIndex + 1];
                if (currentRow.Cells[iCol].Text != "" && currentRow.Cells[iCol].Text != " ")
                {
                    if (currentRow.Cells[iCol].Text == prevRow.Cells[iCol].Text)
                    {
                        currentRow.Cells[iCol].RowSpan = prevRow.Cells[iCol].RowSpan < 1 ? 2 : prevRow.Cells[iCol].RowSpan + 1;
                        prevRow.Cells[iCol].Visible = false;
                    }
                }
            }
        }

        public static void ScrollableTable(GridView gvSource)
        {
            ScrollableTable(gvSource, 0);
        }

        public static void ScrollableTable(GridView gvSource, int iFixedCol)
        {
            string strSuperTableScript = "$(function() { ";
            strSuperTableScript += "      $(\"#" + gvSource.ClientID + "\").toSuperTable({ width: \"" + (gvSource.Width.Value + 10) + "px\", height: \"" + (gvSource.Height.Value + 10) + "px\", fixedCols: " + iFixedCol + "}); ";
            strSuperTableScript += " });";
            ControlHelper.ExecuteScript("SuperTable" + gvSource.ClientID, strSuperTableScript, true);

/*
            if (HttpContext.Current.CurrentHandler is Page)
            {
                Page p = (Page)HttpContext.Current.CurrentHandler;

                if (ScriptManager.GetCurrent(p) != null)
                {
                    //                    ScriptManager.RegisterStartupScript(p, typeof(Page), "SuperTable" + gvSources[0].ClientID, strSuperTableScript, true);                    
                    ScriptManager.RegisterStartupScript(p, typeof(Page), "SuperTable" + gvSource.ClientID, strSuperTableScript, true);
                }
                else
                {
                    //                    p.ClientScript.RegisterStartupScript(typeof(Page), "SuperTable" + gvSources[0].ClientID, strSuperTableScript, true);
                    p.ClientScript.RegisterStartupScript(typeof(Page), "SuperTable" + gvSource.ClientID, strSuperTableScript, true);
                }
            }
 */ 
        }

    }
}
